#include "Movable.h"

#include <string>
#include <Windows.h> // not compatible with Linux
#include <iostream>


Movable::Movable(int x, int y, bool timed, Type type):
	GameObject(x, y, type),
	hVelocity(0),
	vVelocity(0),
	timed(timed),
	moveInterval(rand() % 400 + 300)
{
	timeOfLastMove = GetTickCount(); // Note: this will delay the initial move.
	
	for (int i = 0 ; i < InventoryItem_Count; ++i)
	{
		inventory[i] = 0;
	}
}


void Movable::getDesiredMove(int& hDirection, int& vDirection)
{
	if (isDestroyed())
		return;

	long lCurrent = GetTickCount();
	if (lCurrent >= timeOfLastMove + moveInterval || !timed)
	{
		timeOfLastMove = lCurrent;
		getTimedDesiredMove(hDirection, vDirection);
	}	
}


void Movable::getTimedDesiredMove(int& hDirection, int& vDirection)
{
}


void Movable::getVelocity(int& hCur, int& vCur) const
{
    hCur = hVelocity;
    vCur = vVelocity;
}

    
void Movable::setVelocity(int hNew, int vNew)
{
    hVelocity = hNew;
    vVelocity = vNew;
}


void Movable::setName(const std::string& n)
{
	name = n;
}


std::string Movable::getName() const
{
	return name;
}


void Movable::addToInventory(InventoryItem item, int quantity)
{
	inventory[item] += quantity;
	
	switch (item)
	{
	case InventoryItem_Key:
		std::cout << name << " picked up a key! " << name << " now has " << inventory[item] << " key" << (inventory[item] != 1 ? "s" : "") << "." << std::endl;
		break;
	case InventoryItem_GateKey:
		std::cout << name << " picked up the gate key! Go for it!" << std::endl;
		break;
	case InventoryItem_Gold:
		std::cout << name << " found a treasure of " << quantity << " gold coins. " << name << " now has " << inventory[item] << " gold coins." << std::endl;
		break;
	}
}


bool Movable::hasInInventory(InventoryItem item) const
{
	return inventory[item] > 0;
}


int Movable::getInventoryQuantity(InventoryItem item) const
{
	return inventory[item];
}


void Movable::removeFromInventory(InventoryItem item, int quantity)
{
	if (hasInInventory(item))
	{
		inventory[item] -= quantity;
	
		switch (item)
		{
		case InventoryItem_Key:
			std::cout << name << " used a key! " << name << " now has " << inventory[item] << " key" << (inventory[item] != 1 ? "s" : "") << "." << std::endl;
			break;
		case InventoryItem_GateKey:
			//std::cout << playerName << " lost the gate key! Oh noes!" << std::endl; // Occurs when using the key.
			break;
		case InventoryItem_Gold:
			std::cout << name << " lost " << quantity << " gold coins. " << name << " now has " << inventory[item] << " gold coins." << std::endl;
			break;
		}
	}
}